package com.zjw.handler.security;

import com.alibaba.fastjson.JSON;
import com.zjw.annotation.domain.ResponseResult;
import com.zjw.enums.AppHttpCodeEnum;
import com.zjw.utils.WebUtils;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.security.web.access.AccessDeniedHandler;
import org.springframework.stereotype.Component;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 授权异常处理
 *
 * @author LKZY
 * @date 2023/12/14 15:59
 */
@Component
public class AccessDeniedHandlerImpl implements AccessDeniedHandler {
    @Override
    public void handle(HttpServletRequest httpServletRequest,
                       HttpServletResponse httpServletResponse,
                       AccessDeniedException e)
            throws IOException, ServletException {
        System.out.println("------错误beg--w---↓");
        e.printStackTrace();

        ResponseResult res = ResponseResult.errorResult(AppHttpCodeEnum.NO_OPERATOR_AUTH);
        WebUtils.renderString(httpServletResponse, JSON.toJSONString(res));

    }
}
